package project.Controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import project.Pojo.*;
import project.Pojo.RequestJsonPojo.Body;
import project.Service.UserService;
import project.Util.JwtUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Validated
@RestController
@RequestMapping("/user")
@CrossOrigin
@Tag(name = "用户相关接口")
public class UserController {
    public static String username;

    @Autowired
    @Qualifier("Doctor")     //区分注入的是哪个Service
    UserService<Doctor> doctorService;

    @Autowired
    @Qualifier("Patient")
    UserService<Patient> patientService;


    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public Result register(@RequestBody Body body) {
        String username = body.getUsername();
        String password = body.getPassword();
        int flag = body.getFlag();
        //判断注册用户是病人或是医生 0-病人 1-医生
        if (flag == 0) {
            //判断当前的账号是否已经被注册
            List<Patient> p = patientService.selectByUsername(username);
            //如果结果为空，则该用户不存在，可以继续注册
            if (p.isEmpty()) {
                //把接收到的用户名和密码信息存起来
                patientService.insertAll(username, password);
//                PatientService.insertMessage(username, "", 0);
                //告诉网页，可以注册
                return Result.success();
            } else {
                //如果不为空，则已存在
                return Result.error("该用户已存在于病人表");
            }
            //进行医生账号的判断
        } else {
            List<Doctor> d = doctorService.selectByUsername(username);
            if (d.isEmpty()) {
                doctorService.insertAll(username, password);
//                DoctorService.insertMessage(username, "", 0);
                return Result.success();
            } else {
                return Result.error("该用户已存在于医生表");
            }
        }
    }

    @PostMapping("/login")
    public Result<String> login(@RequestBody Body body, HttpServletRequest request) {
        System.out.println(body);
        username = body.getUsername();
        String password = body.getPassword();
        int flag = body.getFlag();
        //格式校验
        //判断登录用户是病人或是医生 0-病人 1-医生
        if (flag == 0) {
            List<Patient> p = patientService.selectByUsername(username);
            if (!p.isEmpty()) {
                if (p.get(0).getPassword().equals(password)) {
//                    //socket使用,失效
//                    request.getSession().setAttribute("username", username);

//                    用于生成JWT令牌
                    Map<String, Object> claim = new HashMap<>();
//                    将用户名作为JWT令牌的载荷
                    claim.put("username", username);
//                    调用工具类创建jwt令牌
                    String token = JwtUtil.genUtil(claim);
//                    将令牌返回给前端
                    return Result.success(token);
                }
                return Result.error("密码错误");
            } else {
                return Result.error("用户不存在");
            }
        } else {
            List<Doctor> d = doctorService.selectByUsername(username);
            if (!d.isEmpty()) {
                if (d.get(0).getPassword().equals(password)) {
                    request.getSession().setAttribute("username", username);

//                    用于生成JWT令牌
                    Map<String, Object> claim = new HashMap<>();
//                    将用户名作为JWT令牌的载荷
                    claim.put("username", username);
//                    调用工具类创建jwt令牌
                    String token = JwtUtil.genUtil(claim);
//                    将令牌返回给前端
                    return Result.success(token);
                }
                return Result.error("密码错误");
            } else {
                return Result.error("用户不存在");
            }
        }
    }

    // flag = 0 - 病患, flag = 1 - 医生
    @PostMapping("/password")
    public Result rePassword(@RequestBody Body body) {
        String username = body.getUsername();
        String password = body.getPassword();
        int flag = body.getFlag();
        if (flag == 1) {
            doctorService.updatePassword(username, password);
            return Result.success();
        } else {
            patientService.updatePassword(username, password);
            return Result.success();
        }
    }

    @GetMapping("/info")
    Result getUserInfo(int flag) {
        if (flag == 1) {
            List<Doctor> doctors = doctorService.selectByUsername(username);
            Doctor doctor = doctors.get(0);
            return Result.success(doctor);
        } else {
            List<Patient> patients = patientService.selectByUsername(username);
            Patient patient = patients.get(0);
            return Result.success(patient);
        }
    }


    @GetMapping("/getdoctor")
    public Result<List<Doctor>> getAllDoctor() {
        return new Result<>(200, "success", doctorService.selectAll());
    }
}
